package com.zhang.sdk.utils.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.Toast;

/**
 * 吐司工具类
 *
 * 功能：吐司有关功能
 * @author zhang.x
 */
@SuppressLint("ShowToast")
public class ToastUtil {
    private static Toast mToast = null;

    private ToastUtil(){
        throw new UnsupportedOperationException("不能被实例化");
    }

    //取消Toast显示
    public void cancelToast(){
        if(mToast != null){
            mToast.cancel();
        }
    }

    //短时间显示Toast
    public static void showShort(Context context, CharSequence message){
        show(context, message, Toast.LENGTH_SHORT);
    }

    //短时间显示Toast,传入资源ID
    public static void showShort(Context context, int resId){
        show(context, resId, Toast.LENGTH_SHORT);
    }

    //长时显示Toast
    public static void showLong(Context context, CharSequence message){
        show(context, message, Toast.LENGTH_LONG);
    }

    //长时间显示Toast
    public static void showLong(Context context, int resId){
        show(context, resId, Toast.LENGTH_LONG);
    }

    //自定义显示的Toast时间
    public static void show(Context context, CharSequence message, int duration){
        if(mToast == null){
            mToast = Toast.makeText(context, message, duration);
        } else {
            mToast.setText(message);
        }
        mToast.show();
    }

    //自定义显示时间
    public static void show(Context context, int resId, int duration){
         if(mToast == null){
            mToast= Toast.makeText(context, resId, duration);
        }else {
             mToast.setText(resId);
         }
        mToast.show();
    }
}
